package com.netease.financial.base.share.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.netease.financial.R;
import com.netease.financial.base.share.action.BaseShareAction;

public class SharePanelAdapter extends BaseAdapter {

    private BaseShareAction[] mShareActions;
//    private Context mContext;
    private LayoutInflater mInflater;
    private Resources mResources;

    public SharePanelAdapter(Context context, BaseShareAction[] shareActions) {
//        mContext = context;
        mInflater = LayoutInflater.from(context);
        mResources = context.getResources();
        mShareActions = shareActions;
    }

    @Override
    public int getCount() {
        return mShareActions.length;
    }

    @Override
    public BaseShareAction getItem(int position) {
        return mShareActions[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.grid_item, null);
            textView = (TextView) convertView.findViewById(R.id.grid_name);
            convertView.setTag(textView);
        } else {
            textView = (TextView) convertView.getTag();
        }

        textView.setCompoundDrawables(null, getIcon(position), null, null);
        textView.setText(mShareActions[position].getLabelResId());
        return convertView;
    }


    private Drawable getIcon(int position) {
        return getDrawable(mShareActions[position].getIconResId());
    }

    private Drawable getDrawable(int id) {
        Drawable drawable = mResources.getDrawable(id);
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        drawable.setBounds(0, 0, width, height);
        return drawable;
    }

}
